
import Vue from 'vue';
import Vuex from 'vuex';

Vue.use(Vuex);

const store = new Vuex.Store({
    state: {
        // 
        userInfo: JSON.parse(localStorage.getItem("userInfo")) ? JSON.parse(localStorage.getItem("userInfo")) : {},
        token: localStorage.getItem('token') ? localStorage.getItem('token') : '',
        carts: JSON.parse(localStorage.getItem("shopcart")) ? JSON.parse(localStorage.getItem("shopcart")) : {},
        orders: JSON.parse(localStorage.getItem("ordercart")) ? JSON.parse(localStorage.getItem("ordercart")) : {},
        //用来筛选手机的条件
        condition: {
            skuname: '',
            adprice: 0,
            brand: '',
            cpu: '',
            ceamra: '',
            system: '',
            price: [],
            ram: -1,
            rom: -1,
            screen: [],
            ostime: 0,
            commentCounts: 0
        },
        address: '',
        smscondition: {
            forget: false,
            register: false,
            changeInfo: false,
        },
        isloading: true,
    },
    mutations: {
        setToken(state, { token, userInfo }) {
            console.log("setToken")
            state.token = token;
            localStorage.setItem("token", token);     //存储token
            state.userInfo = userInfo;
            localStorage.setItem("userInfo", JSON.stringify(userInfo))
        },
        delToken(state) {
            console.log("sss");
            state.token = '';
            state.userInfo = '';
            localStorage.removeItem("token");    //删除token
            localStorage.removeItem('userInfo')
        },
        setIsloading(state, isloading) {
            state.isloading = isloading
        },
        updataUserInfo(state, userInfo) {
            if (userInfo.logoUrl) {
                state.userInfo.logoUrl = userInfo.logoUrl
            }
            if (userInfo.username) {
                state.userInfo.username = userInfo.username
            }
            localStorage.setItem("userInfo", JSON.stringify(state.userInfo))
        },
        setCondition(state, con) {
            if (con.key == 'skuname') {
                state.condition = {
                    skuname: '',
                    adprice: 0,
                    brand: '',
                    cpu: '',
                    ceamra: '',
                    system: '',
                    price: [],
                    ram: -1,
                    rom: -1,
                    screen: [],
                    ostime: 0,
                    commentCounts: 0
                }
            }
            if (con.key == 'brand') {
                state.condition.skuname = ''
            }
            state.condition[con.key] = con.value

        },
        setCarts(state, carts) {
            state.carts = carts
            localStorage.setItem('shopcart', JSON.stringify(state.carts))
        },
        deleteCart(state) {
            state.carts = ''
            localStorage.removeItem('shopcart')
        },
        setOrders(state, order) {
            state.orders = order
            localStorage.setItem('ordercart', JSON.stringify(state.orders))
        },
        deleteOrder(state) {
            state.orders = ''
            localStorage.removeItem('ordercart')
        },
        setaddress(state, add) {
            state.address = add
        },
        setSmsContion(state, con) {
            state.smscondition[con.key] = con.value
        }
    },
    actions: {
        setaddress(context) {
            context.commit('setaddress')
        }
    },
    getters: {
        GetIsloading: state => {
            return state.isloading
        },
        UserInfo: state => {
            return state.userInfo
        },
        UserInfoID: state => {
            return state.userInfo._id
        },
        UserInfoLogoUrl: state => {
            return state.userInfo.logoUrl
        },
        UserInfoName: state => {
            return state.userInfo.username
        },
        getConditions: state => {
            return state.condition
        },
        getcart: state => {
            return state.carts
        },
        getorder: state => {
            return state.orders
        },
        getaddress: state => {
            return state.address
        },
        getForgetContion: state => {
            return state.smscondition.forget
        },
        getRegisterContion: state => {
            return state.smscondition.register
        },
    }
})
export default store

